//
//  HeaderForCollection.swift
//  许鲜
//
//  Created by hbl on 16/4/22.
//  Copyright © 2016年 hbl. All rights reserved.
//

import UIKit
import AFNetworking

class HeaderForCollection: UIView {
    lazy var dataArray = Array<AnyObject>()
    lazy var imageArray:Array<AnyObject> = Array()
    lazy var pageView:UIScrollView = {
        let s = UIScrollView()
//        s.frame = UIScreen.mainScreen().bounds
        s.bounces = false
        return s
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        //backgroundColor = UIColor.greenColor()
        addSubview(pageView)
        getData()
//        for i in 0...4 {
//            let image = UIImageView()
//            //image.image = UIImage(named: "p\(i + 1)")
//            pageView.addSubview(image)
//            pageView.pagingEnabled = true
//            imageArray.append(image)
//        }
        
//        pageView.contentSize = CGSizeMake(mainW * CGFloat(imageArray.count), 0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        pageView.frame = frame
        let mainW = UIScreen.mainScreen().bounds.size.width
        let imageH = frame.size.height
        
        for i in 0..<imageArray.count {
            let image = imageArray[i] as! UIImageView
            image.frame = CGRectMake(mainW * CGFloat(i), 0, mainW, imageH)
        }
    }
    
    func getData(){
    let manager = AFHTTPSessionManager()
    manager.responseSerializer = AFHTTPResponseSerializer()
    /* cityId	420100
     storeId	285
     token	9984469351296d6352fe44f2ff4ba277*/
    var params = Dictionary<String,String>()
    params["cityId"] = "420100"
    params["storeId"] = "285"
    params["token"] = "9984469351296d6352fe44f2ff4ba277"
    manager.POST("http://mobile.xuxian.com/index/getList/?ver=2016040101", parameters: params, progress: nil, success: { (_, JSON) in
    do{
    
    let str = try NSJSONSerialization.JSONObjectWithData(JSON as! NSData, options: NSJSONReadingOptions.MutableContainers)
    let temp = str["data"] as! Dictionary<String,AnyObject>
    let tempArray = temp["banner"] as! Array<AnyObject>
    //self.centDataArray = CenterModel.mj_objectArrayWithKeyValuesArray(tempArray) as Array
    self.setData(tempArray)
    //print(str)
    }catch{
    
    }
    
    
    }) { (_, error) in
    print(error)
    }
    }
    
    func setData(data:Array<AnyObject>){
        for i in 0..<data.count{
            let str = data[i] as! Dictionary<String, AnyObject>
            let urlStr = str["img"] as! String
            let image = UIImageView()
            image.yy_setImageWithURL(NSURL(string: urlStr), placeholder: nil)
            pageView.addSubview(image)
            pageView.pagingEnabled = true
            imageArray.append(image)
            
           
        }
        pageView.contentSize = CGSizeMake(mainW * CGFloat(imageArray.count), 0)
        self.layoutSubviews()
    }

}
